﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Web;
using WebHacker;
using mshtml;
using log4net;
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]	
namespace ECC_EXT_HttpLib
{

    public class OnlineCheck
    {

        public static ILog logger;
        public OnlineCheck(object obj)
        {

            _DomDocment = obj as HTMLDocument;
        }

        public bool CheckInfo(string str)
        {
            _DomDocment.getElementById("idInput").setAttribute("value", str);

            var v1 =
                _DomDocment.scripts;//??hehe

            string strScript = "fnSelect();";
            foreach (var VARIABLE in v1)
            {
                if ((VARIABLE as HTMLScriptElement).src == null)
                {
                    var res =
                         _DomDocment.parentWindow.execScript(strScript);
                    return true;
                }

            }
            return false;
        }

        public AccountInfo GetProfile()
        {
            AccountInfo cr = new AccountInfo();

            //+Menu
            cr.idName = Utility.GetAttributeById(_DomDocment, "idName", "value");

            cr.idNo = Utility.GetAttributeById(_DomDocment, "idNo", "value");

            cr.idAdd = Utility.GetAttributeById(_DomDocment, "idAdd", "value");

            cr.idTel = Utility.GetAttributeById(_DomDocment, "idTel", "value");

            cr.idAdd2 = Utility.GetAttributeById(_DomDocment, "idAdd2", "value");

            cr.idTel2 = Utility.GetAttributeById(_DomDocment, "idTel2", "value");
            //-Menu

            PersonalInfo _personalInfo;


            //+mail check
            if (!string.IsNullOrEmpty(cr.idAdd))
            {
                try
                {
                    _personalInfo =
                        this.getItems(HttpUtility.UrlEncode("e-mail") + "=" + HttpUtility.UrlEncode(cr.idAdd));
                }catch(Exception ex)
                {
                    logger.Debug("OnlineCheckClass:e-mail");
                    logger.Debug(ex.Message);
                    logger.Debug(ex.StackTrace);
                    Thread.Sleep(500);
                    _personalInfo =
                        this.getItems(HttpUtility.UrlEncode("e-mail") + "=" + HttpUtility.UrlEncode(cr.idAdd));
                }

                cr.checkMail_Name = _personalInfo.Name;
                cr.checkMail_No = _personalInfo.No.Split('　')[0];
                cr.checkMail_strCopy = _personalInfo.StrCopy;
            }
            //-mail check

            //+No check
            if (!string.IsNullOrEmpty(cr.idNo))
            {


                try
                {
                    _personalInfo =
                        this.getItems(HttpUtility.UrlEncode("custno") + "=" + HttpUtility.UrlEncode(cr.idNo));
                }
                catch (Exception ex)
                {
                    logger.Debug("OnlineCheckClass:custno");
                    logger.Debug(ex.Message);
                    logger.Debug(ex.StackTrace);
                    Thread.Sleep(500);
                    _personalInfo =
                        this.getItems(HttpUtility.UrlEncode("custno") + "=" + HttpUtility.UrlEncode(cr.idNo));
                }

                cr.checkNo_Name = _personalInfo.Name;
                cr.checkNo_No = _personalInfo.No.Split('　')[0];
                cr.checkNo_strCopy = _personalInfo.StrCopy;

                cr.DeviceTarget = _personalInfo.DeviceTarget;
                cr.MPS = _personalInfo.MPS;
                cr.HasSystem = _personalInfo.HasSystem;
            }
            //-No check
            
            //+Mail2 check
            if (!string.IsNullOrEmpty(cr.idAdd2))
            {
                //_personalInfo =
                //    this.getItems(HttpUtility.UrlEncode("e-mail") + "=" + HttpUtility.UrlEncode(cr.idAdd2));
                try
                {
                    _personalInfo =
                        this.getItems(HttpUtility.UrlEncode("e-mail") + "=" + HttpUtility.UrlEncode(cr.idAdd2));
                }
                catch (Exception ex)
                {
                    logger.Debug("OnlineCheckClass:e-mail2");
                    logger.Debug(ex.Message);
                    logger.Debug(ex.StackTrace);
                    Thread.Sleep(500);
                    _personalInfo =
                        this.getItems(HttpUtility.UrlEncode("e-mail") + "=" + HttpUtility.UrlEncode(cr.idAdd2));
                }

                cr.checkMail2_Name = _personalInfo.Name;
                cr.checkMail2_No = _personalInfo.No.Split('　')[0];
                cr.checkMail2_strCopy = _personalInfo.StrCopy;
            }
            //-Mail2 check

            return cr;
        }

        private string[] gOnlineKey = new string[] { "顧客姓名(漢字)", "顧客番号", "送付先", "携帯", "ＰＣ" };
        private PersonalInfo getItems(string para)
        {
            PersonalInfo _personalInfo = new PersonalInfo();
            HTMLDocument _htmlDocument = null;

            WebTools wt = new WebTools();
            _htmlDocument =
            wt.SubmitFormData("http://svfile05/scripts/Cust_Inqiry/kensaku3.asp",
                    para);

            int sIdx =
                _htmlDocument.body.outerHTML.IndexOf("clipboardData.setData('Text','");

            if (sIdx != -1)
            {
                _personalInfo.StrCopy = _htmlDocument.body.outerHTML.Substring(sIdx);

                sIdx = _personalInfo.StrCopy.IndexOf("');alert('");
                if (sIdx != -1)
                {
                    _personalInfo.StrCopy = _personalInfo.StrCopy.Substring(0, sIdx).Replace("clipboardData.setData('Text','", "").Replace("\\n", "\n");
                }

            }else
            {
                if (para.Contains("custno"))
                    _personalInfo.Name = "顧客番号の顧客は存在しません";
                else
                    _personalInfo.Name = "E-mailアドレスの顧客は存在しません";

                _personalInfo.No = "";
                _personalInfo.StrCopy = "";
                return _personalInfo;
            }

            HTMLTableCell _htmlHtmlElementClass;
            string[] tmpMobil = new string[] { "", "" };
            string[] tmpPC = new string[] { "", "" };
            foreach (var VARIABLE in _htmlDocument.getElementsByTagName("TH"))
            {
                _htmlHtmlElementClass =
                    VARIABLE as HTMLTableCell;

                //顧客姓名(漢字)
                if (_htmlHtmlElementClass.innerText.IndexOf(gOnlineKey[0]) > -1)
                {


                    var _item =
                    _htmlHtmlElementClass.nextSibling as IHTMLElement;

                    _personalInfo.Name = _item.innerText;

                }

                //顧客番号
                if (_htmlHtmlElementClass.innerText.IndexOf(gOnlineKey[1]) > -1)
                {

                    var _item =
                    _htmlHtmlElementClass.nextSibling as IHTMLElement;

                    _personalInfo.No = _item.innerText;

                }

                //送付先
                if (_htmlHtmlElementClass.innerText.IndexOf(gOnlineKey[2]) > -1)
                {

                    var _item =
                        _htmlHtmlElementClass.nextSibling as IHTMLElement;

                    _personalInfo.DeviceTarget = _item.innerText;

                    if (_personalInfo.DeviceTarget.Contains(gOnlineKey[3]))
                    {
                        _personalInfo.MPS = tmpMobil[0];
                        _personalInfo.HasSystem = tmpMobil[1];
                    }else
                    {
                        _personalInfo.MPS = tmpPC[0];
                        _personalInfo.HasSystem = tmpPC[1];
                    }

                    break;//The Last One
                }



                //"携帯"
                if (_htmlHtmlElementClass.innerText.IndexOf(gOnlineKey[3]) > -1)
                {

                    var _item =
                        _htmlHtmlElementClass.nextSibling as IHTMLElement;

                    int idx =
                        _item.innerHTML.IndexOf("ＭＰＳ区分：");

                    string tmpstr = "";

                    if(idx!=-1)
                        tmpstr = _item.innerHTML.Substring(idx);

                    //this.logger.Debug(tmpstr);

                    idx = tmpstr.IndexOf("&");

                    //this.logger.Debug(idx);

                    tmpstr = tmpstr.Substring(0, idx);

                    //this.logger.Debug(tmpstr);

                    tmpMobil[0] = tmpstr;

                    if (_item.innerText.Contains("ｼｽﾃﾑﾒｰﾙ") || _item.innerText.Contains("システムメール"))
                        tmpMobil[1] = "システムメール : YES";
                    else
                        tmpMobil[1] = "システムメール : NO";
                    //this.logger.Debug(tmpMobil[1]);
                    //this.logger.Debug(_item.innerText);
                }

                //"PC"
                if (_htmlHtmlElementClass.innerText.IndexOf(gOnlineKey[4]) > -1)
                {

                    var _item =
                        _htmlHtmlElementClass.nextSibling as IHTMLElement;

                    int idx =
                       _item.innerHTML.IndexOf("ＭＰＳ区分：");

                    string tmpstr = "";

                    if (idx != -1)
                        tmpstr = _item.innerHTML.Substring(idx);

                    //this.logger.Debug(tmpstr);

                    idx = tmpstr.IndexOf("&");

                    //this.logger.Debug(idx);

                    tmpstr = tmpstr.Substring(0, idx);

                    //this.logger.Debug(tmpstr);

                    tmpPC[0] = tmpstr;

                    if (_item.innerText.Contains("ｼｽﾃﾑﾒｰﾙ") || _item.innerText.Contains("システムメール"))
                        tmpPC[1] = "システムメール : YES";
                    else
                        tmpPC[1] = "システムメール : NO";

                    //this.logger.Debug(tmpPC[1]);
                    //this.logger.Debug(_item.innerText);
                }

            }

            return _personalInfo;
        }

        private HTMLDocument _DomDocment;
    }

    internal class PersonalInfo
    {
        public PersonalInfo()
        {
            this.Name = "";
            this.No = "";
            this.StrCopy = "";
            this.DeviceTarget = "";
            this.MPS = "";
            this.HasSystem = "";
        }

        public string Name { get; set; }
        public string No { get; set; }
        public string StrCopy { get; set; }

        public string DeviceTarget { get; set; }
        public string MPS { get; set; }
        public string HasSystem { get; set; }
            //ｼｽﾃﾑﾒｰﾙ
    }
}
